<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
use think\exception\DbException;
use think\response\Json;

/**
 * 联通光猫
 *
 * @icon fa fa-circle-o
 */
class Luyoultgm extends Backend
{

    /**
     * Luyoultgm模型对象
     * @var \app\admin\model\Luyoultgm
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Luyoultgm;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        $empList = (new \app\admin\model\Luyouemp)->field(['id', 'name'])->whereRaw("FIND_IN_SET('ltgm', flag)")->select();
        if (false === $this->request->isAjax()) {
            $this->view->assign('emp', json_encode($empList));
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }

        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $last = date('t');
        $list = $this->model
            ->where("")
            ->where('day <= '.$last)
            ->order('id','asc')
            ->paginate($limit);
        $input = input();
        if(isset($input['filter']) && !empty(json_decode($input['filter'],true))){
            $filter = json_decode($input['filter'],true);
            if(!empty($filter['day'])) {
                list($s, $e) = explode(' - ', $filter['day']);
                $daylist = getDatesBetween($s, $e);
                //$list = $re;
                $list = [];
                foreach ($daylist as $k => $item) {
                    $list[$k]['day'] = $item;
                }
            }
        }else{
            $new_list = [];
            foreach($list as $k1=>$v1){
                $day = date('Y-m-').$v1->day;
                $d = date('Y-m-d',strtotime($day));
                $new_list[$k1]['day'] = $d;
            }
            $list = $new_list;
        }
//        $lt_yi_arr = [];
//        $lt_zhong_arr = [];
//        $lt_zhuo_arr = [];
//        $lt_wu_arr = [];
//        $lt_yang_arr = [];
//
//        $gd_yi_arr = [];
//        $gd_zhong_arr = [];
//        $gd_zhuo_arr = [];
//        $gd_wu_arr = [];
//        $gd_yang_arr = [];
//        $zongshu_arr = [];

        $total = ['day' => '总数: ', 'guangmao' => 0];
        $typeMap = ['lt' => 1, 'gd' => 2];
        foreach ($list as &$row) {
            $d = $row['day'];

            $row['guangmao'] = 0;
            foreach ($typeMap as $type => $typeVal) {
                foreach ($empList as $emp) {
                    $val = $this->cacl($d, $emp['name'], '11', $typeVal);
                    $key = 'emp_' . $type . '_' . $emp['id'];
                    $row[$key] = $val;
                    $row['guangmao'] += $val;
                    $total['guangmao'] += $val;
                    if (!isset($total[$key])) {
                        $total[$key] = 0;
                    }
                    $total[$key] += $val;
                }
            }
//            $lt_yi = $this->cacl($d,'易剑伟','11',1);
//            $row['lt_yi'] = $lt_yi;
//            $lt_zhong = $this->cacl($d,'钟明锋','11',1);
//            $row['lt_zhong'] = $lt_zhong;
//            $lt_zhuo = $this->cacl($d,'卓大旺','11',1);
//            $row['lt_zhuo'] = $lt_zhuo;
//            $lt_wu = $this->cacl($d,'伍海鹏','11',1);
//            $row['lt_wu'] = $lt_wu;
//            $lt_yang = $this->cacl($d,'杨宇航','11',1);
//            $row['lt_yang'] = $lt_yang;
//
//            $gd_yi = $this->cacl($d,'易剑伟','11',2);
//            $row['gd_yi'] = $gd_yi;
//            $gd_zhong = $this->cacl($d,'钟明锋','11',2);
//            $row['gd_zhong'] = $gd_zhong;
//            $gd_zhuo = $this->cacl($d,'卓大旺','11',2);
//            $row['gd_zhuo'] = $gd_zhuo;
//            $gd_wu = $this->cacl($d,'伍海鹏','11',2);
//            $row['gd_wu'] = $gd_wu;
//            $gd_yang = $this->cacl($d,'杨宇航','11',2);
//            $row['gd_yang'] = $gd_yang;
//            $zongshu = $lt_yi+$lt_zhong+$lt_zhuo+$lt_wu+$lt_yang+$gd_yi+$gd_zhong+$gd_zhuo+$gd_wu+$gd_yang;
//            $row['guangmao'] = $zongshu;
//
//            $lt_yi_arr[] = $lt_yi;
//            $lt_zhong_arr[] = $lt_zhong;
//            $lt_zhuo_arr[] = $lt_zhuo;
//            $lt_wu_arr[] = $lt_wu;
//            $lt_yang_arr[] = $lt_yang;
//
//
//            $gd_yi_arr[] = $gd_yi;
//            $gd_zhong_arr[] = $gd_zhong;
//            $gd_zhuo_arr[] = $gd_zhuo;
//            $gd_wu_arr[] = $gd_wu;
//            $gd_yang_arr[] = $gd_yang;
//            $zongshu_arr[] = $zongshu;

        }
        $list[] = $total;
//        if(!empty($list)){
//            $firstarr[]=[
//                'day'          => '总数: ',
//                'guangmao'          => float_two(array_sum($zongshu_arr)),
//                'lt_yi'          => float_two(array_sum($lt_yi_arr)),
//                'lt_zhong'          => float_two(array_sum($lt_zhong_arr)),
//                'lt_zhuo'          => float_two(array_sum($lt_zhuo_arr)),
//                'lt_wu'          => float_two(array_sum($lt_wu_arr)),
//                'lt_yang'          => float_two(array_sum($lt_yang_arr)),
//                'gd_yi'          => float_two(array_sum($gd_yi_arr)),
//                'gd_zhong'          => float_two(array_sum($gd_zhong_arr)),
//                'gd_zhuo'          => float_two(array_sum($gd_zhuo_arr)),
//                'gd_wu'          => float_two(array_sum($gd_wu_arr)),
//                'gd_yang'          => float_two(array_sum($gd_yang_arr)),
//            ];
//            $list = array_merge($list, $firstarr);
//        }
        $result = array("total" => count($list), "rows" => $list);
        return json($result);
    }


    protected function cacl($d,$user,$luyou_id,$new_hw=0)
    {
        if($new_hw){
            $ronghe = Db::table('fa_luyoukc')
                ->where('opt_date',$d)
                ->where('user',$user)
                ->where('luyou_id',$luyou_id)
                ->where('new_hw',$new_hw)
                //->whereNull('deletetime')
                ->sum('num');
        }else{
            $ronghe = Db::table('fa_luyoukc')
                ->where('user',$user)
                ->where('luyou_id',$luyou_id)
                ->where('opt_date',$d)
                //->whereNull('deletetime')
                ->sum('num');
        }
        return $ronghe==0?'0':float_two($ronghe);
    }

}
